package com.example.dynamicschedule.task;

import com.alibaba.fastjson.JSON;
import com.example.dynamicschedule.config.ScanningCleaningConfig;
import com.example.dynamicschedule.utils.FileDownloadUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class CreditUploadFileTask {
    @Autowired
    private ScanningCleaningConfig scanningCleaningConfig;
    @Autowired
    private FileDownloadUtil fileDownloadUtil;

    static Logger log = Logger.getLogger(UploadFileTask.class);

    /**
     * 三方上传数据
     * @throws Exception
     */
    @Transactional
    public void CreditUploadFileTask() throws Exception {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        String dateStr = simpleDateFormat.format(date);
        log.info("信贷上传三方数据---------------------------开始");
       /* //上传短信
        uploadMessage(dateStr);*/
        //上传智能外呼
        uploadAICall(dateStr);
        //上传人工外呼
        uploadPeopleCall(dateStr);
        log.info("信贷上传三方数据---------------------------结束");
    }

    /**
     * 获取所上传文件名
     *
     * @param txtPath 当地文件地址
     * @return
     */
    private List<String> getFileNameList(String txtPath) {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        String dateStr = simpleDateFormat.format(date);
        File file = new File(txtPath);
        String[] list = file.list();
        log.info("获取文件名称开始");
        List<String> fileList = new ArrayList<>();
        if (list != null) {
            if (list.length > 0) {
                for (String s : list) {
                    if (s.contains(dateStr)) {
                        fileList.add(s);
                    }
                }
            }
        }

        log.info("获取文件名称结束,文件名称为:" + JSON.toJSONString(fileList));
        return fileList;
    }

    /**
     * 人工外呼上传
     * @param dateStr
     */
    private void uploadPeopleCall(String dateStr) {
        log.info("上传人工外呼开始");
        String peopleUploadJsonPath = scanningCleaningConfig.getCreditPeopleUploadJsonPath();
        List<String> peopleUploadFileNameList = getFileNameList(peopleUploadJsonPath);
        if (CollectionUtils.isNotEmpty(peopleUploadFileNameList)) {
            String jsonFileName = null;
            String okFileName = null;
            for (String fileName : peopleUploadFileNameList) {
                if (fileName.contains("json")) {
                    jsonFileName = fileName;
                } else if (fileName.contains("ok")) {
                    okFileName = fileName;
                }
            }
            log.info("人工上传到oss上json文件开始");
            String localFileNameJosnStr = peopleUploadJsonPath + jsonFileName;
            String uploadJsonName = new StringBuilder(scanningCleaningConfig.getPackageName()).append("creditPeople/").append(scanningCleaningConfig.getCreditPrefixUploadJsonPath().replace("yyyyMMdd",dateStr)).toString() ;
            File localFileNameJson = new File(localFileNameJosnStr);
            fileDownloadUtil.uploadOSS(uploadJsonName, localFileNameJson);
            log.info("人工上传到oss上json文件结束,上传文件地址为:"+uploadJsonName);
            log.info("人工上传到oss上ok文件开始");
            String localFileNameOkStr = peopleUploadJsonPath + okFileName;
            String uploadOkName = new StringBuilder(scanningCleaningConfig.getPackageName()).append("creditPeople/").append(scanningCleaningConfig.getCreditPrefixUploadJsonPath().replace("yyyyMMdd",dateStr).replace("json","ok")).toString();
            File localFileNameOk = new File(localFileNameOkStr);
            fileDownloadUtil.uploadOSS(uploadOkName, localFileNameOk);
            log.info("人工上传到oss上ok文件结束,上传文件地址为:"+uploadOkName);
        } else {
            log.info("人工无上传文件");
        }
        log.info("上传人工外呼结束");
    }

    /**
     * 智能外呼上传
     * @param dateStr
     */
    private void uploadAICall(String dateStr) {
        log.info("信贷上传三方数据---------------上传智能外呼--------------开始");
        //获取智能外呼文件名称
        String intelligenceJsonPath = scanningCleaningConfig.getCreditIntelligenceUploadJsonPath();
        List<String> fileNameList = getFileNameList(intelligenceJsonPath);
        if (CollectionUtils.isNotEmpty(fileNameList)) {
            String jsonFileName = null;
            String okFileName = null;
            for (String fileName : fileNameList) {
                if (fileName.contains("json")) {
                    jsonFileName = fileName;
                } else if (fileName.contains("ok")) {
                    okFileName = fileName;
                }
            }
            log.info("信贷上传三方数据---------------智能上传到oss上json文件--------------开始");
            String localFileNameJosnStr = intelligenceJsonPath + jsonFileName;
            String uploadJsonName = new StringBuilder(scanningCleaningConfig.getPackageName()).append("collection_credit_card_intelligence/").append(scanningCleaningConfig.getCreditPrefixUploadJsonPath().replace("yyyyMMdd",dateStr)).toString() ;
            File localFileNameJson = new File(localFileNameJosnStr);
            fileDownloadUtil.uploadOSS(uploadJsonName, localFileNameJson);
            log.info("智能上传到oss上json文件结束,上传文件地址为:"+uploadJsonName);
            log.info("智能上传到oss上ok文件开始");
            String localFileNameOkStr = intelligenceJsonPath + okFileName;
            String uploadOkName = new StringBuilder(scanningCleaningConfig.getPackageName()).append("collection_credit_card_intelligence/").append(scanningCleaningConfig.getCreditPrefixUploadJsonPath().replace("yyyyMMdd",dateStr).replace("json","ok")).toString();

            File localFileNameOk = new File(localFileNameOkStr);
            fileDownloadUtil.uploadOSS(uploadOkName, localFileNameOk);
            log.info("智能上传到oss上ok文件结束,上传文件地址为:"+uploadOkName);
        } else {
            log.info("信贷上传三方数据---------------智能无上传文件--------------开始");
        }
        log.info("信贷上传三方数据---------------上传智能外呼结束--------------开始");
    }

   /* *//**
     * 短信上传
     * @param dateStr
     *//*
    private void uploadMessage(String dateStr) {
        log.info("上传短信文件开始");
        try {
            //获取文件所在的目录
            String txtPath = scanningCleaningConfig.getTxtUploadPath();
            //获取上传文件名
            List<String> messageFileNameList = getFileNameList(txtPath);
            List<String> txtStrList = new ArrayList<>();
            for (String s : messageFileNameList) {
                txtStrList.add(s);
            }
            if (CollectionUtils.isNotEmpty(txtStrList)) {
                ftpUtils.initFtpClient();
                ftpUtils.CreateDirecroty(scanningCleaningConfig.getTxtOnlinePath());
                for (String fileName : txtStrList) {
                    String localFileName = new StringBuilder(txtPath).append(fileName).toString();
                    ftpUtils.uploadFile(scanningCleaningConfig.getTxtOnlinePath(), fileName, localFileName);
                }
            } else {
                log.info("无上传短信文件");
            }

        } catch (IOException e) {
            log.error("上传短信文件失败", e);
        }
        log.info("上传短信文件结束");
    }*/
}
